#include"util.hpp"
#include<vector>
#include<functional>
#include<cstdio>
#include<sys/select.h>

using func_t =std::function<void()> ;
#define INIT(v)\
       do{\
        v.push_back(deletetask);\
        v.push_back(download);\
        v.push_back(executesql);\
       }while(0)
       

#define EXEC_OTHER(cbs)\
    do {                \
        for(auto const &cb:cbs)\
        cb();\
    } while(0)
    

int main()
{ std::vector<func_t> obs;
  INIT(obs);
  setNonBlock(0);//输入端的文件描述符，设置后就是非阻塞
  char bufferr[1024]; 
  while(true)
  {
      ssize_t s=read(0,bufferr,sizeof(bufferr)-1);//从输入端读数据
      if(s>0)
      {
        bufferr[s-1]=0;//读到后显示
        std::cout<<"echo# "<<bufferr<<std::endl;
      }
      else if(s==0)//读取到文件结尾，表示到达文件结尾(EOF)，ctrl+d触发退出循环
      {
        std::cout<<"read end"<<std::endl;
        break;  
     } 
     else{//读取出错
        if(errno==EAGAIN)//处理非阻塞IO的特定错误
        {//没有数据可读
            std::cout<<"没有数据"<<std::endl;
            EXEC_OTHER(obs);//没读到数据执行其它任务
        }
        else if(errno==EINTR)//被信号中断
        {
            continue;//重新读取
        }
        else{//真正的错误
            std::cout<<"s: "<<s<<"errno: "<<strerror(errno)<<std::endl;
            break;
        }

     }
  sleep(1);

  }
  return 0;

}